package com.mtask.web.controller;

import com.mtask.common.constants.InformerEnum;
import com.mtask.common.dto.Result;
import com.mtask.common.dto.vo.NotificationVo;
import com.mtask.common.dto.vo.SystemConfVo;
import com.mtask.common.dto.vo.VersionVo;
import com.mtask.task.service.impl.QueueMessageSender;
import com.mtask.web.service.HomeService;
import com.mtask.web.service.ProjectService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/home")
@ResponseBody
public class HomeController {
    @Resource
    private HomeService homeService;
    @Resource
    private ProjectService projectService;

    @Resource
    private QueueMessageSender sender;

    @GetMapping
    public List<SystemConfVo> index() {
        return homeService.getAllSystemConfig();
    }


    @GetMapping("/add")
    public SystemConfVo add() {
        return homeService.addSystemConfig();
    }

    @GetMapping("/version")
    public Result<List<VersionVo>> getProjectVersion() {
        return new Result<>(200, "ok", projectService.getProjectVersion());
    }

    @GetMapping("/message")
    public Result<String> message() {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            NotificationVo notification = new NotificationVo();
            notification.setContent("测试内容");
            notification.setContentType("text");
            notification.setInformMethod(InformerEnum.Email);
            sender.send("item.test", notification);
        }
        return new Result<>(200, "ok", "发送成功用时:" + (System.currentTimeMillis() - start));
    }
}
